package com.xpsoft.framework.json;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
 * 用于格式化Date对象在json中的显示格式
 * @Class Name DateJsonValueProcessor
 * @Author likang
 * @Create In Dec 1, 2010
 */
public class DateJsonValueProcessor implements JsonValueProcessor {
	//默认现实格式
	public static final String Default_DATE_PATTERN = "yyyy-MM-dd hh:mm:ss";

	private DateFormat dateFormat;

	public DateJsonValueProcessor(String datePattern) {
		try {
			dateFormat = new SimpleDateFormat(datePattern);
		} catch (Exception e) {
			dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
		}
	}

	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	public Object processObjectValue(String key, Object value,
			JsonConfig jsonConfig) {
		return process(value);
	}

	private Object process(Object value) {
		return dateFormat.format((Date) value);
	}

}
